/*
 * Filename OrderController.java 2022年03月10日
 * Copyright © Ehome Co.Ltd. All Rgiths Reserved.
 * @author wuyingchao
 */
package com.ehome.poct.core.controller;

import com.ehome.poct.core.api.constant.enums.DeviceCheckStatus;
import com.ehome.poct.core.api.dto.check.CheckStatusDTO;
import com.ehome.poct.core.api.dto.check.TestUniRecordDTO;
import com.ehome.poct.core.api.dto.order.TestOrderDTO;
import com.ehome.poct.core.api.dto.order.TestOrderItemDTO;
import com.ehome.poct.core.api.virtual.TestOrderService;
import com.ehome.poct.core.api.vo.order.OrderItemPriceVO;
import com.ehome.poct.core.api.vo.order.OrderStatusVO;
import com.ehome.poct.core.api.vo.order.QrCodeVO;
import com.ehome.poct.core.api.vo.order.TestOrderVO;
import com.ehome.web.annotation.RequestLog;
import com.ehome.web.controller.AbstractBaseController;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author wyc
 * @since 2022/03/10 11:04
 **/
@RestController
@AllArgsConstructor
@Api(tags = "检测订单")
@ApiSupport(author = "wyc")
@RequestMapping("/orders")
public class OrderController extends AbstractBaseController<TestOrderVO, TestOrderDTO> {
    private final TestOrderService orderService;

    /**
     *
     * @param order
     */
    @ApiOperation("保存订单")
    @ApiOperationSupport(author = "wyc")
    @PostMapping("/place")
    @RequestLog
    public QrCodeVO saveWithItem(@RequestBody TestOrderDTO order,Boolean isPay){
        return orderService.place(order,isPay);
    }

    @ApiOperation("重新下单")
    @ApiOperationSupport(author = "wyc")
    @GetMapping("/pay/link/{orderNo}")
    @RequestLog
    public QrCodeVO getPayLink(@PathVariable String orderNo){
        return orderService.getPayLink(orderNo);
    }

    @ApiOperation("查询订单状态")
    @ApiOperationSupport(author = "wyc")
    @GetMapping("/poll/status/{orderNo}")
    public OrderStatusVO pollGetStatus(@PathVariable String orderNo) {
        return orderService.getOrderStatus(orderNo);
    }

    @ApiOperation("查询支付中心订单状态")
    @ApiOperationSupport(author = "wyc")
    @GetMapping("/manual/status/{orderNo}")
    @RequestLog
    public OrderStatusVO manualGetStatus(@PathVariable String orderNo) {
        return orderService.getOrderFromCenter(orderNo);
    }

    @ApiOperation("删除订单")
    @ApiOperationSupport(author = "wyc")
    @DeleteMapping("/delete/byOrderNo/{orderNo}")
    public void delByOrderNo(@PathVariable String orderNo) {
        orderService.delByOrderNo(orderNo);
    }

    @ApiOperation("联机检测下单（金标仪）")
    @ApiOperationSupport(author = "wyc")
    @PostMapping(value = "/union/place/{recordId}", params = "type=1")
    @RequestLog
    public QrCodeVO unionPlace4Jby(@PathVariable Long recordId, @RequestBody TestOrderDTO order) {
        return orderService.unionPlace(order,
                new TestUniRecordDTO().setTestUniRecordId(recordId).setJbyFinishTime(LocalDateTime.now()).setJbyStatus(DeviceCheckStatus.FINISH.getType()));
    }

    @ApiOperation("联机检测下单（血常规）")
    @ApiOperationSupport(author = "wyc")
    @PostMapping(value = "/union/place/{recordId}", params = "type=2")
    @RequestLog
    public QrCodeVO unionPlace4Bld(@PathVariable Long recordId, @RequestBody TestOrderDTO order) {
        return orderService.unionPlace(order,
                new TestUniRecordDTO().setTestUniRecordId(recordId).setXcgFinishTime(LocalDateTime.now()).setXcgStatus(DeviceCheckStatus.FINISH.getType()));
    }

    @ApiOperation("联机检测预下单（金标仪）")
    @ApiOperationSupport(author = "wyc")
    @PostMapping(value = "/union/pre/place/{recordId}", params = "type=1")
    @RequestLog
    public void prePlace4Jby(@PathVariable Long recordId, @RequestBody TestOrderDTO order) {
        orderService.prePlace(order,
                new TestUniRecordDTO().setTestUniRecordId(recordId).setJbyFinishTime(LocalDateTime.now()).setJbyStatus(DeviceCheckStatus.FINISH.getType()));
    }

    @ApiOperation("联机检测预下单（血常规）")
    @ApiOperationSupport(author = "wyc")
    @PostMapping(value = "/union/pre/place/{recordId}", params = "type=2")
    @RequestLog
    public void prePlace4Bld(@PathVariable Long recordId, @RequestBody TestOrderDTO order) {
        orderService.prePlace(order,
                new TestUniRecordDTO().setTestUniRecordId(recordId).setXcgFinishTime(LocalDateTime.now()).setXcgStatus(DeviceCheckStatus.FINISH.getType()));
    }

    @ApiOperation("修改订单状态")
    @ApiOperationSupport(author = "muqiu")
    @PostMapping("/updateOrderStatus/{orderNo}")
    public void updateOrderStatus(@PathVariable String orderNo){
        orderService.updateOrderStatus(orderNo);
    }

    @ApiOperation("获取订单号对应的 订单项 价格信息")
    @GetMapping("/orderitemprice/{orderNo}")
    public List<OrderItemPriceVO> orderItemInfo(@PathVariable String orderNo){
        return orderService.getOrderItemPriceInfo(orderNo);
    }

    @ApiOperation("获取订单价格扩展信息")
    @PostMapping("/extInfo")
    public List<OrderItemPriceVO> extInfoWithOrder(@RequestBody List<TestOrderItemDTO> items) {
        return orderService.getPriceItemByOrderItem(items);
    }

    @PostMapping("/updateByOrderNo")
    public void updateByOrderNo(@RequestBody CheckStatusDTO checkStatusDTO){
        orderService.updateByOrderNo(checkStatusDTO);
    }
}